package com.weixing.mall.provider.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.provider.api.model.vo.SysUserEditVo;
import com.weixing.mall.provider.api.service.SysConfigServiceFeign;
import com.weixing.mall.provider.mapper.AfterSaleMapper;
import com.weixing.mall.provider.mapper.AfterSaleOperationMapper;
import com.weixing.mall.provider.model.domain.AfterSale;
import com.weixing.mall.provider.model.domain.AfterSaleOperation;
import com.weixing.mall.provider.model.dto.AfterSaleOperationDto;
import com.weixing.mall.provider.model.dto.CustomerOperationDto;
import com.weixing.mall.provider.model.query.AfterSaleOperationQuery;
import com.weixing.mall.provider.model.vo.AfterSaleOperationVo;
import com.weixing.mall.provider.model.vo.CustomerDevelopmentTimeVo;
import com.weixing.mall.provider.model.vo.DevelopmentTimeVo;
import com.weixing.mall.provider.service.IAfterSaleOperationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName AfterSaleOperationServiceImpl
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/10/11
 * @Version 1.0
 **/

/**
 * <p>
 * 订单模块-售后操作记录表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-20
 */
@Service
public class AfterSaleOperationServiceImpl extends ServiceImpl<AfterSaleOperationMapper, AfterSaleOperation> implements IAfterSaleOperationService {

    @Autowired
    private AfterSaleOperationMapper afterSaleOperationMapper;
    @Autowired
    private AfterSaleMapper afterSaleMapper;
    @Autowired
    private SysConfigServiceFeign sysConfigService;

    @Override
    public List<AfterSaleOperationVo> selectList(AfterSaleOperationQuery afterSaleOperationQuery) {
        return afterSaleOperationMapper.selectAfterSaleOperationList(afterSaleOperationQuery);
    }

    @Override
    public void save(AfterSaleOperationDto afterSaleOperationDto, Long uid) {
        AfterSaleOperation afterSaleOperation = new AfterSaleOperation();
        BeanUtils.copyProperties(afterSaleOperationDto, afterSaleOperation);
        afterSaleOperation.setOperatorId(uid);
        if (null == afterSaleOperationDto.getId()) {
            save(afterSaleOperation);
        } else {
            updateById(afterSaleOperation);
        }
    }

    @Override
    public void saveCustomer(CustomerOperationDto customerOperationDto) {
//        Long userId = RequestUtil.getCurrentUserId(); // TODO ResultUtil成功时要解开
        Long userId = 1L;
        //通过id查名称（user表）
        SysUserEditVo sysUserEditVo = sysConfigService.getUserById(userId).getData();
        AfterSaleOperation afterSaleOperation = new AfterSaleOperation();
        //售后id
        afterSaleOperation.setAfterSaleId(customerOperationDto.getAfterSaleId());
        //操作员id
        afterSaleOperation.setOperatorId(userId);
        //操作员名称
        if(sysUserEditVo != null){
            afterSaleOperation.setOperator(sysUserEditVo.getRealName());
        }
        //操作员类型
        if(customerOperationDto.getOperatorType()!=null ){
            afterSaleOperation.setOperatorType(customerOperationDto.getOperatorType().getValue());
        }
        //审核结果
        if(customerOperationDto.getAuditResult() != null){
            afterSaleOperation.setAuditResult(customerOperationDto.getAuditResult().getValue());
        }
        //审核备注
        if(customerOperationDto.getAuditResult()!=null){
            afterSaleOperation.setAuditRemark(customerOperationDto.getAuditResult().getDesp());
        }
        //操作备注
        afterSaleOperation.setOperatorRemark(customerOperationDto.getOperatorRemark());

        //保存记录
        save(afterSaleOperation);
    }

    @Override
    public DevelopmentTimeVo developmentTime(Long afterSaleId) {
        List<AfterSaleOperation> list = afterSaleOperationMapper.selectList(new QueryWrapper<AfterSaleOperation>().eq("after_sale_id", afterSaleId));
        DevelopmentTimeVo developmentTimeVo = new DevelopmentTimeVo();
        AfterSale afterSale = afterSaleMapper.selectById(afterSaleId);
        if(list == null || list.size() == 0 ){
            return null;
        }
        for(int i=0;i<list.size();i++){
            if(ObjectUtil.isEmpty(list.get(i))){
                continue;
            }
            if(list.get(i).getAuditResult()==0){
                developmentTimeVo.setBuyerReturn("用户退回");
                developmentTimeVo.setBuyerReturnTime(afterSale.getCreatedTime());
            }else if(list.get(i).getAuditResult()==7){
                developmentTimeVo.setPreliminaryExamination("初审通过");
                developmentTimeVo.setPreliminaryExaminationTime(list.get(i).getCreatedTime());
            }else if(list.get(i).getAuditResult()==8){
                developmentTimeVo.setReexamine("复审通过");
                developmentTimeVo.setReexamineTime(list.get(i).getCreatedTime());
            }else if(list.get(i).getAuditResult()==9){
                developmentTimeVo.setPreliminaryExamination("初审失败");
                developmentTimeVo.setPreliminaryExaminationTime(list.get(i).getCreatedTime());
            }else if(list.get(i).getAuditResult()==10){
                developmentTimeVo.setReexamine("复审失败");
                developmentTimeVo.setReexamineTime(list.get(i).getCreatedTime());
            }else if(list.get(i).getAuditResult()==9&&list.get(i+1).getAuditResult()==1){
                developmentTimeVo.setPreliminaryExamination("客服审核通过");
                developmentTimeVo.setPreliminaryExaminationTime(list.get(i).getCreatedTime());
            }else if(list.get(i).getAuditResult()==10&&list.get(i+1).getAuditResult()==1){
                developmentTimeVo.setReexamine("客服审核通过");
                developmentTimeVo.setReexamineTime(list.get(i).getCreatedTime());
            }else if(list.get(i).getAuditResult()==1){
                developmentTimeVo.setCustomerAudit("客服审核通过");
                developmentTimeVo.setCustomerAuditTime(list.get(i).getCreatedTime());
            }else if(list.get(i).getAuditResult()==2){
                developmentTimeVo.setCustomerAudit("客服审核拒绝");
                developmentTimeVo.setCustomerAuditTime(list.get(i).getCreatedTime());
            }else if(list.get(i).getAuditResult()==11){
                developmentTimeVo.setShipper("发货部发货");
                developmentTimeVo.setShipperTime(list.get(i).getCreatedTime());
            }
        }

        return developmentTimeVo;
    }

    @Override
    public CustomerDevelopmentTimeVo customerDevelopmentTime(Long afterSaleId) {
        List<AfterSaleOperation> list = afterSaleOperationMapper.selectList(new QueryWrapper<AfterSaleOperation>().eq("after_sale_id", afterSaleId));
        CustomerDevelopmentTimeVo customerDevelopmentTimeVo = new CustomerDevelopmentTimeVo();
        for(int i=0;i<list.size();i++){
            if(list.get(i).getAuditResult()==8){
                customerDevelopmentTimeVo.setAppAudit("app仓审核通过");
                customerDevelopmentTimeVo.setAppAuditTime(list.get(i).getCreatedTime());
            }else if(list.get(i).getAuditResult()==10&&list.get(i+1).getAuditResult()==1){
                customerDevelopmentTimeVo.setAppAudit("app仓审核通过");
                customerDevelopmentTimeVo.setAppAuditTime(list.get(i).getCreatedTime());
            }else if(list.get(i).getAuditResult()==1){
                customerDevelopmentTimeVo.setCustomerAudit("客服审核通过");
                customerDevelopmentTimeVo.setCustomerAuditTime(list.get(i).getCreatedTime());
            } else if(list.get(i).getAuditResult()==2){
                customerDevelopmentTimeVo.setCustomerAudit("客服审核拒绝");
                customerDevelopmentTimeVo.setCustomerAuditTime(list.get(i).getCreatedTime());
            }else if(list.get(i).getAuditResult()==0){
                customerDevelopmentTimeVo.setBuyerReturn("客户申请");
                customerDevelopmentTimeVo.setBuyerReturnTime(list.get(i).getCreatedTime());
            }else if(list.get(i).getAuditResult()==11){
                customerDevelopmentTimeVo.setShipper("发货部发货");
                customerDevelopmentTimeVo.setShipperTime(list.get(i).getCreatedTime());
            }
            AfterSale afterSale = afterSaleMapper.selectById(list.get(i).getAfterSaleId());
            if(afterSale.getStatus()==9){
                customerDevelopmentTimeVo.setReceivingGoods("确认收货");
                customerDevelopmentTimeVo.setReceivingTime(afterSale.getUpdatedTime());
            }
        }
        return customerDevelopmentTimeVo;
    }
}
